﻿CREATE PROCEDURE [dbo].[proc_Finance_RunningAccount_Update]
	(
		@TradeDate nvarchar(10),
		@Summary nvarchar(50),
		@AccountsTypeId int,
		@ParentId int,
		@ParentName nvarchar(50),
		@SubjectId int,
		@SubjectName nvarchar(50),
		@PaymentTypeId int,
		@PaymentTypeName nvarchar(50),
		@BankId int,
		@BankName nvarchar(50),
		@Money numeric(18,2),
		@Agent nvarchar(50),
		@AgentName nvarchar(50),
		@Description nvarchar(200),
		@BillNumber nvarchar(50),
		@RId UNIQUEIDENTIFIER,
		@CompanyId int
	)
AS
begin
	Declare @Income numeric(18,2),@Outlay numeric(18,2)
	if(@AccountsTypeId=1)
		begin
			Set @Income=@Money
			Set @Outlay=0
		end
	else
		begin
			Set @Income=0
			Set @Outlay=@Money
		end
	
	Update Finance_RunningAccount Set
		TradeDate=@TradeDate,
		Summary=@Summary,
		ParentId=@ParentId,
		ParentName=@ParentName,
		SubjectId=@SubjectId,
		SubjectName=@SubjectName,
		PaymentTypeId=@PaymentTypeId,
		PaymentTypeName=@PaymentTypeName,
		BankId=@BankId,
		BankName=@BankName,
		Income=@Income,
		Outlay=@Outlay,
		Agent=@Agent,
		AgentName=@AgentName,
		Description=@Description,
		BillNumber=@BillNumber,
		CompanyId = @CompanyId
		Where RId=@RId
		
		
	--修改Finance_RunningAccount时需要同时修改Finance_RunningAccountCopy 1.10加
	UPDATE [Finance_RunningAccountCopy]
   SET   
       [TradeDate] = @TradeDate
      ,[Summary] = @Summary
      ,[PaymentTypeId] =@PaymentTypeId
      ,[PaymentTypeName] = @PaymentTypeName
      ,[BankId] = @BankId
      ,[BankName] = @BankName
      ,[Income] = @Income
      ,[Outlay] = @Outlay
      ,[Description] = @Description
      ,[BillNumber] = @BillNumber
      ,CompanyId = @CompanyId
 WHERE [Finance_RunningAccountCopy].RId=(SELECT id FROM Finance_RunningAccount WHERE Finance_RunningAccount.RId=@RId)
end
	RETURN
